统计信息:字数 9298 阅读19分钟
This is a cookbook of how to get things done with webpack. This includes most things we use at Instagram and nothing we don't use.My advice: start with this as your webpack docs, then look at the official docs for clarification.
这是Instagram公司的使用的webpack配置,可以从这个文档看起,然后以官方网站为准。
It supports AMD and CommonJS, among other module systems (Angular, ES6). If you don't know what to use, use CommonJS.
These are equivalent:
However, webpack is more powerful than Browserify, so you generally want to make a webpack.config.js to keep things organized:
This is just JS, so feel free to put Real Code in there.
相对于 browserify , webpack 可以设置配置文件
怎样使用 webpack? 在目录中增加配置文件,打包时可以增加参数配置不同的打包环境
Switch to the directory containing webpack.config.js and run:
webpack for building once for developmentwebpack -p for building once for production (minification)webpack --watch for continuous incremental build in development (fast!)webpack -d to include source maps怎样编译成JS文件(typescript coffeescript ES6 编译成 Js)各种 loader 预处理
webpack's equivalent of browserify transforms and RequireJS plugins is a loader. Here's how you can teach webpack to load CoffeeScript and Facebook JSX+ES6 support (you must npm install babel-loader coffee-loader):
See also the babel-loader installation instructions for additional dependencies (tl;dr run npm install babel-core babel-preset-es2015 babel-preset-react).
To enable requiring files without specifying the extension, you must add a resolve.extensions parameter specifying which files webpack searches for:
First update your code to require() your static assets (named as they would with node's require()):
When you require CSS (or less, etc), webpack inlines the CSS as a string inside the JS bundle and require() will insert a <style> tag into the page. When you require images, webpack inlines a URL to the image into the bundle and returns it from require().
But you need to teach webpack to do this (again, with loaders):
We have code we want to gate only to our dev environments (like logging) and our internal dogfooding servers (like unreleased features we're testing with employees). In your code, refer to magic globals:
Then teach webpack those magic globals:
Then you can build with BUILD_DEV=1 BUILD_PRERELEASE=1 webpack from the console. Note that since webpack -p runs uglify dead-code elimination, anything wrapped in one of these blocks will be stripped out, so you won't leak secret features or strings.
Let's say you have a profile page and a feed page. You don't want to make the user download the code for the feed if they just want the profile. So make multiple bundles: create one "main module" (called an entrypoint) per page:
For profile, insert <script src="build/Profile.js"></script> into your page. Do a similar thing for feed.
Feed and Profile share a lot in common (like React and the common stylesheets and components). webpack can figure out what they have in common and make a shared bundle that can be cached between pages:
Add <script src="build/common.js"></script> before the script tag you added in the previous step and enjoy the free caching.
CommonJS is synchronous but webpack provides a way to asynchronously specify dependencies. This is useful for client-side routers, where you want the router on every page, but you don't want to have to download features until you actually need them.
Specify the split point where you want to load asynchronously. For example:
webpack will do the rest and generate extra chunk files and load them for you.
webpack will assume that those files are in your root directory when you load then into a html script tag for example. You can use output.publicPath to configure that.
Take a look at a real world example on how a successful team is leveraging webpack: http://youtu.be/VkTCL6Nqm6Y This is Pete Hunt at OSCon talking about webpack at Instagram.com
webpack is extremely modular. What makes webpack great is that it lets plugins inject themselves into more places in the build process when compared to alternatives like browserify and requirejs. Many things that may seem built into the core are just plugins that are loaded by default and can be overridden (i.e. the CommonJS require() parser).